package org.footito.android.dialog;

import org.footito.android.R;
import org.footito.android.application.FootitoApplication;
import org.footito.android.connexion.Connexion;
import org.footito.android.connexion.ConnexionListener;
import org.footito.android.tools.Settings;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class ConnexionDialog extends Dialog implements android.view.View.OnClickListener, OnFocusChangeListener, ConnexionListener {

	private static final String TAG = "ConnexionDialog";
	private Button bOk;
	private Button bCancel;
	private EditText nickEdit;
	private EditText passwordEdit;
	private ProgressBar progressConnexion;
	private Connexion connexion;
	private TextView errorLabel;

	public ConnexionDialog(Context context) {
		super(context);
		initDialog(context);
	}

	public ConnexionDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		initDialog(context);
	}

	public ConnexionDialog(Context context, int theme) {
		super(context, theme);
		initDialog(context);
	}
	

	public void resumeDialog() {
		errorLabel.setVisibility(View.GONE);
		progressConnexion.setVisibility(View.GONE);
	}

	private void initDialog(Context context) {
		setContentView(R.layout.dialog_connexion);
		setTitle(R.string.connexionTitle);
		connexion = FootitoApplication.getApplicationInstance().getConnexion();

		errorLabel = (TextView) findViewById(R.id.errorLabel);
		
		nickEdit = (EditText) findViewById(R.id.nickEdit);
		nickEdit.setText(Settings.getNickName(context));
		nickEdit.setOnFocusChangeListener(this);

		passwordEdit = (EditText) findViewById(R.id.passwordEdit);

		progressConnexion = (ProgressBar) findViewById(R.id.progressConnexion);

		bOk = (Button) findViewById(R.id.bOk);
		bOk.setOnClickListener(this);

		bCancel = (Button) findViewById(R.id.bCancel);
		bCancel.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v == bCancel) {
			this.dismiss();
		} else {
			connexion.setPassword(passwordEdit.getText().toString());
			connexion.setLogin(nickEdit.getText().toString());
			progressConnexion.setVisibility(View.VISIBLE);
			connexion.connect(this);
		}
	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		if (v == nickEdit && !hasFocus) {
			Settings.setNickName(v.getContext(), nickEdit.getText().toString());
		}
	}

	@Override
	public void onConnexionFinished(int state) {
		progressConnexion.setVisibility(View.GONE);
		if (state == 0) {
			errorLabel.setVisibility(View.VISIBLE);
		} else {
			Toast.makeText(getContext(), R.string.youareconnected, Toast.LENGTH_SHORT).show();
			dismiss();
		}
	}

	@Override
	public void onConnexionFailed(Exception exception) {
		Toast.makeText(getContext(), R.string.connexionImpossible, Toast.LENGTH_SHORT).show();
		this.dismiss();
	}

}
